<html>
<head>
	<meta charset="UTF-8">
	<meta content="IE=edge" http-equiv="X-UA-Compatible">
	<meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width" name="viewport">
	<title>1401：Hexagon</title>
	<!-- css -->
	<link href="../css/base.min.css" rel="stylesheet">
	<link href="../css/project.min.css" rel="stylesheet">
	
	<!-- favicon -->
	<!-- ... -->
</head>
<body class="page-brand">
	<header class="header header-transparent header-waterfall ui-header">
		<ul class="nav nav-list pull-left">
			<li>
				<a data-toggle="menu" href="#menu">
					<span class="icon icon-lg">menu</span>
				</a>
			</li>
		</ul>
		<a class="header-logo header-affix-hide margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">Hexagon</a>
		<span class="header-logo header-affix margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">Hexagon</span>
	</header>
	<nav aria-hidden="true" class="menu" id="menu" tabindex="-1">
		<div class="menu-scroll">
			<div class="menu-content">
				<a class="menu-logo" href="../index.html">BZOJ离线题库</a>
				<ul class="nav">
					<li>
						<a class="waves-attach" data-toggle="collapse" href="#problems">题目</a>
						<ul class="menu-collapse collapse in" id="problems">
							<li>
								<a class="waves-attach" href="../index.html">主页</a>
							</li>
							<li>
								<a class="waves-attach" href="../list.html">题目列表</a>
							</li>
						</ul>
					</li>
					<li>
						<a class="collapsed waves-attach" data-toggle="collapse" href="#about">关于</a>
						<ul class="menu-collapse collapse" id="about">
							<li>
								<a class="waves-attach" href="../about.html">关于此项目</a>
							</li>
						</ul>
					</li>
					
				</ul>
			</div>
		</div>
	</nav>
	<main class="content">
		<div class="content-header ui-content-header">
			<div class="container">
				<h1 class="content-heading">
                Hexagon                </h1>
                <p>时间限制：3s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  空间限制：64MB</p>			</div>
		</div>
		<div class="container">
			<section class="content-inner margin-top-no">
				<div class="row">
					<div class="col-lg-13 col-md-13">
						<div class="card margin-bottom-no">
							<div class="card-main">
								<div class="card-inner">
									
                                <h3>题目描述</h3><p><p><span style="font-size: medium">A civil engineer that has recently graduated from the Czech Technical University encountered an interesting problem and asked us for a help. The problem is more of economical than engineering nature. The engineer needs to connect several buildings with an infrastructure. Unfortunately, the investor is not the owner of all the land between these places. Therefore, some properties have to be bought first. The land is divided into a regular ``grid&quot; of hexagonal parcels, each of them forms an independent unit and has the same value. Some of the parcels belong to the investor. These parcels form four connected areas, each containing one building to be connected with the others. Your task is to find the minimal number of parcels that must be acquired to connect the four given areas. The whole land also has a hexagonal shape with six sides, each consisting of exactly H parcels. The above picture shows a land with H = 4 , parcels with letters represent the four areas to be connected. In this case, it is necessary to buy four additional parcels. One of the possible solutions is marked by crosses.</span></p>
<div style="text-indent: 21pt"><span style="font-size: medium">&ldquo;话说有一天下流星雨，天上掉下来一个玉佩，它拥有无穷的力量。不过由于进入大气层的时候受到过于大的压力而导致被分成了4个部分散落在人间&hellip;&hellip;&rdquo;</span></div>
<div style="text-indent: 21pt"><span style="font-size: medium">春哥对你说了这个传说，你抱着&ldquo;信春鸽，得永生&rdquo;的信念来到了这块散落着的玉佩的土地上。</span></div>
<div style="text-indent: 21pt"><span style="font-size: medium">这是一块正六边形的土地，到达的时候你发现了它已经变成了4个部分分散在各个角落。经过探索你发现了这个玉佩只有把4块全部连到一起的时候才会发出无穷的力量，但是由于某种因素和技术原因，我们并不能移动任意一个玉佩的部分，只能通过购买一种连接用材料铺盖在土地上，把它们全部连在一起才行。</span></div>
<div style="text-indent: 21pt"><span style="font-size: medium">现在需要找到最少要多少钱才可以得到这无穷的力量。</span></div>
<div style="text-indent: 21pt"><span style="font-size: medium">&nbsp;</span></div>
<div style="text-indent: 21pt"><span style="font-size: medium">比如说下图（见hint），玉佩被分成了4个部分ABCD，最少需要铺盖4块土地（用x标出）就可以把它们全部连接起来。</span></div></p><hr/><h3>输入格式</h3><p><p><span style="font-size: medium">The input contains several scenarios. Each scenario begins with an integer number H , which specifies the size of the land, 2 H 20 . Then there are 2*H - 1 lines representing individual ``rows&quot; of the land (always oriented as in the picture). The lines contain one non-space character for each parcel. It means the first line will contain H characters, the second line H + 1 , and so on. The longest line will be the middle one, with 2*H - 1 characters. Then the ``length&quot; descends and the last line contains H parcels, again. The character representing a parcel will be either a dot (``.&quot;) for the land that is not owned by the investor, or one of the uppercase letters ``A&quot;, ``B&quot;, ``C&quot;, or ``D&quot;. The areas of parcels occupied by the same letter will always be connected. It means that between any two parcels in the same area, there exists a path leading only through that area. Beside the characters representing parcels, the lines may contain any number of spaces at any positions to improve ``human readability&quot; of the input. There is always at least one space between two letters (or the dots). After the land description, there will be one empty line and then the next scenario begins. The last scenario is followed by a line containing zero. </span></p></p><hr/><h3>输出格式</h3><p><p><span style="font-size: medium">For each scenario, output one line with the sentence ``You have to buy P parcels.&quot;, where P is the minimal number of parcels that must be acquired to make all four areas connected together. Areas are considered connected, if it is possible to find a path between them that leads only through parcels that have been bought. </span></p></p><hr/><h3>样例输入</h3><pre>4 
    B . . C 
   . . . . C 
  . A . . C . 
 . A A . . . .
  . A . . . . 
   . . . D D 
    . . . . 
0
</pre><hr/><h3>样例输出</h3><pre>You have to buy 4 parcels.
</pre><hr/><h3>提示</h3><p><p><img border="0" alt="" src="../file/1401_0.jpg" /></p></p><hr/><h3>题目来源</h3><p>没有写明来源</p>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				
			</section>
		</div>
	</main>

	<div class="fbtn-container">
		<div class="fbtn-inner">
			<a class="fbtn fbtn-lg fbtn-brand-accent waves-attach waves-circle waves-light waves-effect" data-toggle="dropdown" aria-expanded="true"><span class="fbtn-text fbtn-text-left">Menu</span><span class="fbtn-ori icon">apps</span><span class="fbtn-sub icon">close</span></a>
			<div class="fbtn-dropup">
				<a class="fbtn fbtn-brand waves-attach waves-circle waves-light waves-effect" href="../list.html" target="_self"><span class="fbtn-text fbtn-text-left">题目列表</span><span class="icon">menu</span></a>
				<a class="fbtn fbtn-green waves-attach waves-circle waves-effect" href="../index.html" target="_self"><span class="fbtn-text fbtn-text-left">返回主页</span><span class="icon">home</span></a>
				<a class="fbtn waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/submitpage.php?id=1401" target="_blank"><span class="fbtn-text fbtn-text-left">提交代码</span><span class="icon">send</span></a>
				<a class="fbtn fbtn-orange waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=1401" target="_blank"><span class="fbtn-text fbtn-text-left">试题讨论</span><span class="icon">chat</span></a>
				
			</div>
		</div>
	</div>

	<!-- js -->
	<script src="../js/jquery.min.js"></script>
	<script src="../js/base.min.js"></script>
	<script src="../js/project.min.js"></script>
</body>
</html>